#include <iostream>
#include <cerrno>
#include <string>

#include "sock.hpp"

void usage(std::string proc)
{
  std::cout << "usage:\n\t" << proc << ": need server_ip server_port" << std::endl;
}

int main(int argc, char* argv[])
{
  if (argc != 3)
  {
    usage(argv[0]);
    return 1;
  }

  int sock = Sock::Socket();
  Sock::Connect(sock, argv[1], atoi(argv[2]));

  while(true)
  {
    char buffer[1024]; 
    ssize_t ret = read(sock, buffer, sizeof(buffer) - 1);
    if (ret > 0)
    {
        std::cout << "ret > 0" << std::endl;
    }
    //加上这句,可以看见server的TIME_WAIT状态,client的CLOSED状态
    // else if (ret == 0)
    // {
    //     close(sock);
    //     return 0;
    // }
    //不加的话,则是server的FIN_WAIT_2状态,client的CLOSE_WAIT状态
    //以上都是以server主动退出产生的效果
    else
    {
      perror("read");
      std::cout << "errno : " << errno << std::endl;
      sleep(1);
    }
  }

  return 0;
}
